package demo02;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/*
 *  转换文件编码：
 *  案例分析：指定GBK编码的输入转换流，读取文本文件，再指定UTF-8编码的输出转换流，写入文件
 */
public class Demo03ChangeCharset {
    public static void main(String[] args) {
        try (InputStreamReader inputStreamReader = new InputStreamReader( // 以gbk编码输入
                new FileInputStream("file\\charsetTxt\\changeGbk.txt"), "gbk");
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter( // 以utf-8编码输出
                        new FileOutputStream("file\\charsetTxt\\changeUtf8.txt"), "utf-8")) {
            int len = 0;
            char[] chars = new char[1024];
            while ((len = inputStreamReader.read(chars)) != -1) {
                outputStreamWriter.write(chars, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
